package pteam;

import java.util.*;
/**
 *  Decision Class
 *
 *  Decision is an implementation of a node in a decision tree logical 
 *  structure that represents a need for the robot to turn a given direction 
 *  at a given point along its path. 
 *  Decisions are aware of subsequent decisions, as well as prior decisions
 *  selected to reach this.
 *  FROZEN!!!!!!!
 *  This is a data structure that shouldn't be altered for any purpose
 */
public class Decision {
public static final int NORTH = 0;
public static final int WEST = 1;
public static final int SOUTH = 2;
public static final int EAST = 3;
  
  private int camefromdir;
  private int optiondir;	 //Direction of opening in grid square represented by this
  private boolean selected; // currently along the branch of decisions made so far
  private boolean fail; // previously tried decision that lead to a dead end
  public ArrayList<Decision> subsequent;	//Decisions following this
 
  protected Stack<Decision> prior;     //Top of stack contains most recent decision
  //TODO swap generics for casting in stack.
  public Decision(int dir, int camefrom){
     /*optiondir=dir;
     selected=false;
     prior=new Stack<Decision>();
     subsequent=new ArrayList<Decision>();
     distance=-1;*/
     this(dir, new Stack<Decision>(), camefrom);
     fail=false;
  }

   public Decision(int dir, Stack<Decision> s, int camefrom){
    optiondir=dir;
    prior=s;
    selected=false;
    subsequent=new ArrayList<Decision>();
    camefromdir=camefrom;
    fail=false;
   }
   


  public ArrayList<Decision> getChoices() {
    return subsequent;
  }

  public Stack<Decision> getPrior() { return prior;	}
  public void select(){
    selected=true;
    prior.push(this);
  }
    
  public void fail(){     //undo it, but remain in tree
    if(selected){
      selected=false;
      prior.pop();	//Remove this from decisions made
      fail=true;
    }
  }
  public void addChoice(Decision d){
        d.setPrior(this.prior);
	subsequent.add(d);
  }
  public void removeChoice(Decision d){
	d.setFail(true);
  }
  public int getOptionDir() { //trueness
    return optiondir;
  }
  public void setOptionDir(int optiondir) {
	this.optiondir = optiondir;
  }
  public boolean isSelected() {
	return selected;
  }
  public void setPrior(Stack<Decision> prior) {
	this.prior = prior;
  }

  
  public boolean noSubsequent() {
    boolean trueness = false;
    if (subsequent.isEmpty()) return false;
    for (int i = 0; i < subsequent.size(); i++) {
      if(subsequent.get(i).getFail());
      else return true;
    }
    return trueness;
  }
  public int getDirection() { return optiondir; } //added to make files build - is this incorrect?
  public int getCameFrom(){
    return camefromdir;
  }
  public boolean getFail() { return fail;}
  public void setFail(boolean b) {fail = b;}
}
